package com.imooc.authorization.dao;

import com.imooc.authorization.common.BaseDAO;
import com.imooc.authorization.entity.RoleFunction;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Administrator on 2016/8/21.
 */

@Repository
public class RoleFunctionDAO extends BaseDAO{

    private class RoleFunctionMapper implements RowMapper<RoleFunction> {


        @Override
        public RoleFunction mapRow(ResultSet rs, int rowNum) throws SQLException {
            RoleFunction roleFunction = new RoleFunction();
            roleFunction.setId(rs.getLong("id"));
            roleFunction.setRoleId(rs.getLong("role_id"));
            roleFunction.setFunctionId(rs.getLong("function_id"));
            roleFunction.setStatus(rs.getInt("status"));

            return roleFunction;
        }
    }



    /**
     * 根据ID查询角色功能对应关系
     * @param id 角色功能对应关系ID
     * @return 角色功能对应关系
     */
    public RoleFunction findRoleFunctionById(Long id){
        String sql ="select * from auth_role_function where id = ?";

        return jdbcTemplate.queryForObject(sql,new Object[]{id},new RoleFunctionMapper());
    }

    /**
     * 批理保存角色功能对应关系
     * @param roleFunctions 角色功能对应关系集合
     */
    public void saveRoleRunctions(Collection<RoleFunction> roleFunctions){
        String sql ="insert into auth_role_function(role_id,function_id,status) values(?,?,?)";
        List<Object[]> batchArgs = new ArrayList<>();
        roleFunctions.forEach((rf) -> {
            Object[] objs = new Object[3];
            objs[0] = rf.getRoleId();
            objs[1] = rf.getFunctionId();
            objs[2] =rf.getStatus();

            batchArgs.add(objs);
        });

        jdbcTemplate.batchUpdate(sql,batchArgs);

    }

    /**
     * 根据角色ID查询角色功能对应关系集合
     * @param roleId 角色ID
     * @return 角色功能对应关系集合
     */
    public List<RoleFunction> findRoleFunctionsByRoleId(Long roleId){

      String sql = "select * from auth_role_function where role_id=?";

        return jdbcTemplate.query(sql,new Object[]{roleId},new RoleFunctionMapper());
    }

    /**
     * 根据角色ID删除角色功能对应关系
     * @param roleId 角色ID
     */
    public void deleteByRoleId(Long roleId){

        String sql = "delete from auth_role_function where role_id=?";

        jdbcTemplate.update(sql,roleId);


    }


}
